package my.syncfilter.entity;

public enum FieldType {
    MAX("MAX"),
    MIN("MIN"),
    STRING("STRING"),
    INTEGER("INTEGER"),
    DATE("DATE");

    private String type;

    private FieldType(String type) {
        this.type = type;
    }

    public String getType() {
        return this.type;
    }

    public static FieldType get(String s) {
        if (null==s) {
            return null;
        }
        if (s.toUpperCase().startsWith("MAX")) {
            return MAX;
        }
        if (s.toUpperCase().startsWith("MIN")) {
            return MIN;
        }
        if (s.toUpperCase().startsWith("STRING")) {
            return STRING;
        }
        if (s.toUpperCase().startsWith("INTEGER")) {
            return INTEGER;
        }
        if (s.toUpperCase().startsWith("DATE")) {
            return DATE;
        }
        return null;
    }

    /**
     * 响应比较结果
     *
     * @param a
     * @param b
     * @return
     */
    public Object compareRes(Object a, Object b) {
        if (b == null) {
            return a;
        }

        if (a instanceof Long) {
            if (this.getType().equals("MAX")) {
                return Long.max((Long) a, (Long) b);
            } else {
                return Long.min((Long) a, (Long) b);
            }
        }

        if (a instanceof Integer) {
            if (this.getType().equals("MAX")) {
                return Integer.max((Integer) a, (Integer) b);
            } else {
                return Integer.min((Integer) a, (Integer) b);
            }
        }
        return 0;
    }
}
